#
# ttkiosk - table tennis club touch screen based kiosk.
# Copyright (C) 2009  Sergey Satskiy
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA  02110-1301, USA.
#
# $Id: Logo.py 31 2009-10-26 04:07:07Z sergey.satskiy $
#
# Generated by PyQt4 UI code generator 4.5.4 and then customized
#


from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import SIGNAL
import ui
from utils import debugMsg, GlobalData


class ClickableLogoLabel( QtGui.QLabel ):
    """ The class is only for the club logo label """

    def __init__( self, firstLabel ):
        QtGui.QLabel.__init__( self, firstLabel )

    def mouseReleaseEvent( self, event ):
        """ go home """

        ui.navigateHome()
        if GlobalData().isAdmin:
            ui.hideForm( 'TopBar' )
            ui.showForm( 'AdminTopBar' )
        return


class Ui_Logo(ui.FormBaseClass):
    def setupUi(self, Logo, path):
        ui.FormBaseClass.__init__(self)

        Logo.setObjectName("Logo")
        Logo.resize(400, 300)
        self.gridLayoutWidget = QtGui.QWidget(Logo)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 351, 171))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setObjectName("gridLayout")
        #self.logoPicture = QtGui.QLabel(self.gridLayoutWidget)
        self.logoPicture = ClickableLogoLabel(self.gridLayoutWidget)

        self.updatePicture( path )

        self.logoPicture.setObjectName("logoPicture")
        self.gridLayout.addWidget(self.logoPicture, 1, 1, 1, 1)
        spacerItem = QtGui.QSpacerItem(5, 20, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem, 1, 0, 1, 1)
        spacerItem1 = QtGui.QSpacerItem(20, 5, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        self.gridLayout.addItem(spacerItem1, 0, 1, 1, 1)
        spacerItem2 = QtGui.QSpacerItem(20, 5, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        self.gridLayout.addItem(spacerItem2, 2, 1, 1, 1)
        spacerItem3 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem3, 1, 2, 1, 1)

        self.retranslateUi(Logo)
        QtCore.QMetaObject.connectSlotsByName(Logo)

    def retranslateUi(self, Logo):
        Logo.setWindowTitle(QtGui.QApplication.translate("Logo", "Form", None, QtGui.QApplication.UnicodeUTF8))

    def updatePicture( self, path ):
        pictureName = ""
        if GlobalData().isConnected:
            pictureName = path + 'logo.gif'
        else:
            pictureName = path + 'warning.png'
        self.logoPicture.setPixmap(QtGui.QPixmap(pictureName))
        return


class Logo(QtGui.QWidget, Ui_Logo):
    def __init__(self, path, parent=None, f=QtCore.Qt.WindowFlags()):
        QtGui.QWidget.__init__(self, parent, f)

        self.path = path
        self.setupUi(self, path)

        self.connect( GlobalData().application,
                      SIGNAL( "connectionStatus" ),
                      self.onConnectionChanged )
        return

    def setLayoutGeometry( self, width, height ):
        """ updates the whole form layout size """

        self.gridLayoutWidget.setGeometry( QtCore.QRect( 0, 0, width, height ) )
        return

    def onConnectionChanged( self ):
        self.updatePicture( self.path )
        return


